package com.sdry.web.controller.lz;

import com.sdry.model.lz.*;
import com.sdry.service.lz.MaintResumeService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @ClassName MaintResumeController
 * @Description 设备维护维修记录
 * @Author lz
 * @Date 2018年10月10日 10:23:02
 * @Version 1.0
 */
@RequestMapping("/maintResume")
@Controller
public class MaintResumeController {

    public static final Integer AFFECT_ROW = 0;
    @Resource
    MaintResumeService maintResumeService;

    /**
     * 设备事故报告添加
     * @param response
     * @param accidentReport
     * @throws Exception
     */
    @RequestMapping("/addMaintenanceResume")
    public void addMaintenanceResume(HttpServletResponse response, AccidentReport accidentReport) throws Exception {
    	if (accidentReport != null) {
			if(StringUtil.isEmpty(accidentReport.getAccidentTime())){
				accidentReport.setAccidentTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.getdTime())){
				accidentReport.setdTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.geteTime())){
				accidentReport.seteTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.getmTime())){
				accidentReport.setmTime(null);
			}
		}
        Long affectRow = maintResumeService.addMaintenanceResume(accidentReport);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteMaintenanceResumeById")
    public void deleteDeviceById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = maintResumeService.deleteMaintenanceResumeById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改事故报告
     * @param response
     * @param accidentReport
     * @throws Exception
     */
    @RequestMapping("/editMaintenanceResumeById")
    public void editMaintenanceResumeById(HttpServletResponse response, AccidentReport accidentReport) throws Exception {
    	if (accidentReport != null) {
			if(StringUtil.isEmpty(accidentReport.getAccidentTime())){
				accidentReport.setAccidentTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.getdTime())){
				accidentReport.setdTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.geteTime())){
				accidentReport.seteTime(null);
			}
			if(StringUtil.isEmpty(accidentReport.getmTime())){
				accidentReport.setmTime(null);
			}
		}
        Long affectRow = maintResumeService.editMaintenanceResumeById(accidentReport);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备事故
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryMaintenanceResume")
    public void queryMaintenanceResume(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<AccidentReport> accidentList = maintResumeService.queryMaintenanceResume(criteria);
        int count = maintResumeService.countMaintenanceResume(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(accidentList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设备维修申请表添加
     * @param response
     * @param maintenanceRecord
     * @throws Exception
     */
    @RequestMapping("/addMaintenanceRecord")
    public void addMaintenanceRecord(HttpServletResponse response,
                                     MaintenanceRecord maintenanceRecord) throws Exception {
    	if (maintenanceRecord != null) {
			if(StringUtil.isEmpty(maintenanceRecord.getMaintainTime())){
				maintenanceRecord.setMaintainTime(null);
			}
		}
        Long affectRow1 = maintResumeService.addMaintenanceRecord(maintenanceRecord);
        
        /*maintenanceRecordDetail.setmId(affectRow1);*/
        /*if(maintenanceRecordDetail.getMaintainItem() != null){
        	String [] maintainItem = maintenanceRecordDetail.getMaintainItem().split(",");
        	for (int i = 0; i < maintainItem.length; i++) {
        		maintenanceRecordDetail.setMaintainItem(maintainItem[i]);
        	}
        } else {
        	maintenanceRecordDetail.setMaintainItem("");
        }
        
        if(maintenanceRecordDetail.getResult() != null){
        	String [] result = maintenanceRecordDetail.getResult().split(",");
        	for (int i = 0; i < result.length; i++) {
        		maintenanceRecordDetail.setResult(result[i]);
        	}
        } else {
        	maintenanceRecordDetail.setResult("");
        }
        
        if(maintenanceRecordDetail.getRemark() != null){
        	String [] remark = maintenanceRecordDetail.getRemark().split(",");
        	for (int i = 0; i < remark.length; i++) {
        		maintenanceRecordDetail.setRemark(remark[i]);
        	}
        } else {
        	maintenanceRecordDetail.setRemark("");
        }*/
        
        /*for (int i = 0; i < maintainItem.length ; i++) {
            maintenanceRecordDetail.setmId(affectRow1);
            maintenanceRecordDetail.setMaintainItem(maintainItem[i]);
            maintenanceRecordDetail.setResult(result[i]);
            maintenanceRecordDetail.setRemark(remark[i]);
            maintResumeService.addMaintenanceRecordDetail(maintenanceRecordDetail);
        }*/
        if (affectRow1 > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow1);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 添加保养项目详细
     * @param maintenanceRecordDetail
     */
    @RequestMapping("/addMaintenanceRecordDetail")
    public void addMaintenanceRecordDetail(MaintenanceRecordDetail maintenanceRecordDetail){
    	maintResumeService.addMaintenanceRecordDetail(maintenanceRecordDetail);
    }
    
    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteMaintenanceRecordById")
    public void deleteMaintenanceRecordById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = maintResumeService.deleteMaintenanceRecordById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备维修申请表
     * @param response
     * @param maintenanceRecord
     * @throws Exception
     */
    @RequestMapping("/editMaintenanceRecordById")
    public void editMaintenanceRecordById(HttpServletResponse response, MaintenanceRecord maintenanceRecord) throws Exception {
    	if (maintenanceRecord != null) {
			if(StringUtil.isEmpty(maintenanceRecord.getMaintainTime())){
				maintenanceRecord.setMaintainTime(null);
			}
		}
        Long affectRow = maintResumeService.editMaintenanceRecordById(maintenanceRecord);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询维修申请登记
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryMaintenanceRecord")
    public void queryMaintenanceRecord(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<MaintenanceRecord> recordList = maintResumeService.queryMaintenanceRecord(criteria);
        int count = maintResumeService.countMaintenanceRecord(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(recordList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设备保养记录表添加
     * @param response
     * @param deviceMaintenanceRecords
     * @throws Exception
     */
    @RequestMapping("/addDeviceMaintenanceRecords")
    public void addDeviceMaintenanceRecords(HttpServletResponse response,
                                            DeviceMaintenanceRecords deviceMaintenanceRecords,
                                            DeviceMaintenanceRecordsDetail deviceMaintenanceRecordsDetail) throws Exception {
    	if (deviceMaintenanceRecords != null) {
			if(StringUtil.isEmpty(deviceMaintenanceRecords.getRepairTime())){
				deviceMaintenanceRecords.setRepairTime(null);
			}
		}
        Long affectRow1 = maintResumeService.addDeviceMaintenanceRecords(deviceMaintenanceRecords);
        String [] deviceName = deviceMaintenanceRecordsDetail.getChangeDeviceName().split(",");
        String [] deviceVersion = deviceMaintenanceRecordsDetail.getDeviceVersion().split(",");
        String [] deviceCount = deviceMaintenanceRecordsDetail.getDeviceCount().split(",");
        String [] deviceUnit = deviceMaintenanceRecordsDetail.getDeviceUnit().split(",");
        for (int i = 0; i < deviceName.length ; i++) {
            deviceMaintenanceRecordsDetail.setmId(affectRow1);
            deviceMaintenanceRecordsDetail.setChangeDeviceName(deviceName[i]);
            deviceMaintenanceRecordsDetail.setDeviceVersion(deviceVersion[i]);
            deviceMaintenanceRecordsDetail.setDeviceCount(deviceCount[i]);
            deviceMaintenanceRecordsDetail.setDeviceUnit(deviceUnit[i]);
            maintResumeService.addDeviceMaintenanceRecordsDetail(deviceMaintenanceRecordsDetail);
        }
        if (affectRow1 > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow1);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteDeviceMaintenanceRecordsById")
    public void deleteDeviceMaintenanceRecordsById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = maintResumeService.deleteDeviceMaintenanceRecordsById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备维修申请表
     * @param response
     * @param deviceMaintenanceRecords
     * @throws Exception
     */
    @RequestMapping("/editDeviceMaintenanceRecordsById")
    public void editDeviceMaintenanceRecordsById(HttpServletResponse response, DeviceMaintenanceRecords deviceMaintenanceRecords) throws Exception {
        Long affectRow = maintResumeService.editDeviceMaintenanceRecordsById(deviceMaintenanceRecords);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备保养记录
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryDeviceMaintenanceRecords")
    public void queryDeviceMaintenanceRecords(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceMaintenanceRecords> deviceRecordList = maintResumeService.queryDeviceMaintenanceRecords(criteria);
        int count = maintResumeService.countDeviceMaintenanceRecords(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceRecordList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 根据申请表ID查询保养详细
     * @param response
     * @param mId 当前申请表ID
     */
    @RequestMapping("/queryMaintenanceRecordDetailById")
    public void queryMaintenanceRecordDetailById(HttpServletResponse response, String mId) throws Exception {
        List<MaintenanceRecordDetail> maintenanceRecordDetailList = maintResumeService.queryMaintenanceRecordDetailById(Long.parseLong(mId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(maintenanceRecordDetailList);
        if (maintenanceRecordDetailList.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }
    
    
    /**
     * 根据保养记录表ID查询更换设备详细(安卓接口)
     * @param response
     * @param mId 当前保养记录表ID
     */
    @RequestMapping("/queryDeviceMaintenanceRecordDetailById")
    public void queryDeviceMaintenanceRecordDetailById(HttpServletResponse response, String mId) throws Exception {
        List<DeviceMaintenanceRecordsDetail> deviceMaintenanceRecordDetailList = maintResumeService.queryDeviceMaintenanceRecordDetailById(Long.parseLong(mId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceMaintenanceRecordDetailList);
        if (deviceMaintenanceRecordDetailList.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }
    
    /**
     * 设备事故报告等级(安卓接口)
     * @param response
     * @throws Exception
     */
    @RequestMapping("/queryWaitRepairDeviceInter")
    public void queryWaitRepairDeviceInter(HttpServletResponse response) throws Exception {
    	List<AccidentReport> maintenanceRecordList = maintResumeService.queryWaitRepairDeviceInter();
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(maintenanceRecordList);
        if (maintenanceRecordList.size() > AFFECT_ROW){
            jsonObject.put("state", 1);
            jsonObject.put("message", "查找成功");
            jsonObject.put("data", jsonArray);
            ResponseUtil.write(response, jsonObject);
        } else {
        	jsonObject.put("state", 0);
            jsonObject.put("message", "查找失败");
            jsonObject.put("data", null);
            ResponseUtil.write(response, jsonObject);
        }
    }
    
    /**
     * 设备维修申请登记(安卓接口)
     * @param response
     * @throws Exception
     */
    @RequestMapping("/queryAccidentReportInter")
    public void queryAccidentReportInter(HttpServletResponse response) throws Exception {
    	List<DeviceBreakdown> maintenanceRecordList = maintResumeService.queryAccidentReportInter();
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(maintenanceRecordList);
        if (maintenanceRecordList.size() > AFFECT_ROW){
            jsonObject.put("state", 1);
            jsonObject.put("message", "查找成功");
            jsonObject.put("data", jsonArray);
            ResponseUtil.write(response, jsonObject);
        } else {
        	jsonObject.put("state", 0);
            jsonObject.put("message", "查找失败");
            jsonObject.put("data", null);
            ResponseUtil.write(response, jsonObject);
        }
    }
}
